package cn.com.haopy.yue.ai.db.service.hos.impl;

import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskUserHumanSource;
import cn.com.haopy.yue.ai.db.mapper.arisk.AIRiskUserHumanSourceMapper;
import cn.com.haopy.yue.ai.db.service.BaseServiceImpl;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskUserHumanSourceService;
import cn.com.haopy.yue.ai.db.utils.SqlInjectionExtUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/4/14 17:24
 */
@Service
public class AIRiskUserHumanSourceServiceImpl extends BaseServiceImpl<AIRiskUserHumanSourceMapper, AIRiskUserHumanSource>
        implements AIRiskUserHumanSourceService {
    @Override
    public AIRiskUserHumanSource getByUserSourceId(Integer userId, String sourceId, String sign) {
        LambdaQueryWrapper<AIRiskUserHumanSource> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(AIRiskUserHumanSource::getUserId, userId);
        if (Objects.nonNull(sourceId)) {
            queryWrapper.eq(AIRiskUserHumanSource::getSourceId, sourceId);
        }
        if (Objects.nonNull(sign)) {
            queryWrapper.eq(AIRiskUserHumanSource::getSign, sign);
        }

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public AIRiskUserHumanSource getBySourceId(String sourceId, String sign) {
        LambdaQueryWrapper<AIRiskUserHumanSource> queryWrapper = new LambdaQueryWrapper<>();

        if (Objects.nonNull(sourceId)) {
            queryWrapper.eq(AIRiskUserHumanSource::getSourceId, sourceId);
        }
        if (Objects.nonNull(sign)) {
            queryWrapper.eq(AIRiskUserHumanSource::getSign, sign);
        }

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer getCountUseIng() {
        return super.baseMapper.getCountUseIng();
    }

    @Override
    public Page<AIRiskUserHumanSource> querySelective(Integer page, Integer limit, Boolean isFinish, String sort, String order) {
        if (SqlInjectionExtUtils.check(sort) || SqlInjectionExtUtils.check(order)) {
            throw new RuntimeException("存在非法字段");
        }
        QueryWrapper<AIRiskUserHumanSource> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(isFinish)) {
            queryWrapper.lambda().eq(AIRiskUserHumanSource::getIsFinish, isFinish);
        }

        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }
        return super.page(new Page<>(page, limit), queryWrapper);
    }

    @Override
    public int endUseAll() {
        return super.baseMapper.endUseAll();
    }
}
